<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

setcookie('user', '', time() - 3600);

include_once 'private.inc.php';

include_once 'controller.inc.php';
$db_link = db_connect();
$all_users = db_getAllUsers();
db_disconnect($db_link);

$username = $_POST['username'];
$password = $_POST['password'];
$password_hash = encrypt_password($password);

$valid = FALSE;
foreach ($all_users as $user) {
  if ($user->getEmail() == $username && 
      $user->getPassword() == $password_hash) {
    setcookie('user', $user->getEmail(), time() + 7200);
    $valid = TRUE;
  }
}

if ($valid) {
  header('Location: dashboard.php');
}
else {
  header('Location: index.php?failure=login');
}

?>
